﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace syc.Store
{
    public partial class 原料到货记录 : System.Web.UI.Page
    {
        protected bool IsReadonly = false;
        protected bool HeGe = false;
        protected string Notice = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            string[] list;
            int bid = 0;
            int p=0;
            if (!this.IsPostBack)
            {
                //权限管理子程序==========================================================
                string opp3 = (string)Session["OPP3"];
                if (opp3 == null || opp3 == ",")
                {
                    this.LeiBie.DataBind();
                }
                else
                {
                    if (opp3.StartsWith(","))
                    {
                        opp3 = opp3.Substring(1);
                    }
                    if (opp3.EndsWith(","))
                    {
                        opp3 = opp3.Substring(0, opp3.Length - 1);
                    }
                    this.DS_LeiBie.SelectCommand = "select ID, [品名]+' '+[钢级]+' '+[规格] AS 名称 FROM 原料 where [ID] in (" + opp3 + ")";
                }
                this.LeiBie.DataBind();
                //权限管理子程序==========================================================
                if (Request.QueryString["ID"] != null)
                {
                    int.TryParse(Request.QueryString["ID"], out bid);
                }
                if (Request.QueryString["p"] != null)
                {
                    int.TryParse(Request.QueryString["p"], out p);
                }
                if (Request.QueryString["list"] != null)
                {
                    list = Request.QueryString["list"].Split(new char[]{','});
                    int n;
                    bool ok = true;
                    for (int i = 0; i < list.Length; i++)
                    {
                        if (!int.TryParse(list[i], out n))
                        {
                            ok = false;
                        }
                    }
                    if (ok)
                    {
                        if (p < 0)
                        {
                            p = 0;
                        }
                        else if (p > list.Length - 1)
                        {
                            p = list.Length;
                        }
                        if (bid <= 0)
                        {
                            int.TryParse(list[p], out bid);
                        }
                        this.Label_List.Text = string.Join(",", list);

                        if (p > 0)
                        {
                            this.Button_Prev.Visible = true;
                        }
                        if (p < list.Length - 1)
                        {
                            this.Button_Next.Visible = true;
                        }
                        this.Button_Send.Visible = true;

                        this.Label_PageNotice.Text = "第" + (p + 1) + "张 共" + list.Length + "张";
                        this.Button_Next.CommandArgument = p.ToString();
                        
                    }
                }
                
                if (bid > 0)
                {
                    this.Label_ID.Text = bid.ToString();
                    SqlConnection Conn = new SqlConnection();
                    Conn.ConnectionString = ConfigurationManager.ConnectionStrings["SCMConnectionString1"].ConnectionString;
                    Conn.Open();
                    SqlCommand Cmd = new SqlCommand();
                    Cmd.Connection = Conn;

                    Cmd.CommandText = "select * from 原料到货记录 where ID=" + bid;
                    Cmd.CommandType = CommandType.Text;
                    SqlDataReader Reader = Cmd.ExecuteReader();
                    if (Reader.Read())
                    {
                        this.LeiBie.Text = ((int)Reader["原料ID"]).ToString();
                        this.GongHuo.Text = ((int)Reader["供货ID"]).ToString();
                        this.FenChang.Text = ((int)Reader["分厂"]).ToString();
                        this.Date1.Value = ((DateTime)Reader["日期"]).ToLongDateString();
                        this.KuQu.Value = ((int)Reader["库区"]).ToString();
                        this.BangZhong1.Value = ((decimal)Reader["磅重1"]).ToString();
                        this.BangZhong2.Value = ((decimal)Reader["磅重2"]).ToString();

                        if (!Convert.IsDBNull(Reader["车号"]))
                        {
                            this.CheHao.Value = (String)Reader["车号"];
                        }
                        if (!Convert.IsDBNull(Reader["备注"]))
                        {
                            this.BeiZhu.Value = (String)Reader["备注"];
                        }

                        if ((byte)Reader["状态"] > 0)
                        {
                            if ((byte)Reader["状态"] > 1)
                            {
                                this.HeGe = true;
                                this.PDZ.Visible = true;
                            }
                            this.IsReadonly = true;
                            this.FenChang.Enabled = false;
                            this.GongHuo.Enabled = false;
                            this.LeiBie.Enabled = false;
                            this.Button_Save.Enabled = false;
                            this.Button_Save.Visible = false;
                        }

                        Reader.Close();
                        Cmd.CommandText = "select rtrim(炉号) as 炉号,rtrim(批次) as 批次,重量,支数 from 原料到发明细 where 记录ID=" + bid;
                        Cmd.CommandType = CommandType.Text;
                        Reader = Cmd.ExecuteReader();

                        while (Reader.Read())
                        {
                            this.LuHao.Value += (string)Reader["炉号"] + "|";
                            this.PiCi.Value += (string)Reader["批次"] + "|";
                            this.ZhongLiang.Value += ((decimal)Reader["重量"]).ToString() + "|";
                            this.ZhiShu.Value += ((int)Reader["支数"]).ToString() + "|";
                        }
                    }
                    Reader.Close();

                    Conn.Close();
                    Reader.Dispose();
                    Cmd.Dispose();
                    Conn.Dispose();
                }
                else
                {
                    this.Date1.Value = DateTime.Today.ToLongDateString();
                }
            }
        }

        protected void Button_Save_Click(object sender, EventArgs e)
        {
            DateTime d;
            if (!DateTime.TryParse(this.Date1.Value, out d))
            {
                this.Notice = "“日期”录入有误。";
                return;
            }
            decimal bz1,bz2=0;
            if (!decimal.TryParse(this.BangZhong1.Value, out bz1))
            {
                this.Notice = "“磅量1”录入有误。";
                return;
            }
            else if (bz1 <= 0)
            {
                this.Notice = "“磅量1”需录入一个正数。";
                return;
            }

            decimal.TryParse(this.BangZhong2.Value, out bz2);
            if (bz2 < 0)
            {
                this.Notice = "“磅量2”需录入一个正数。";
                return;
            }

            int kq;
            if (this.LeiBie.Text == "5" || this.LeiBie.Text == "6")
            {
                if (this.KuQu.Value == "")
                {
                    this.KuQu.Value = "0";
                }
                if (this.KuQu.Value != "0")
                {
                    this.Notice = "成品接箍的“存储库区”应填0或者不填写";
                    return;
                }
                kq = 0;
            }
            else if (!int.TryParse(this.KuQu.Value, out kq))
            {
                this.Notice = "“库区”录入有误。";
                return;
            }
            else if (kq <= 0 || kq > 100)
            {
                this.Notice = "“库区”需录 1 - 100 的正整数。";
                return;
            }

            string op = (string)this.Session["OperatorName"];
            if (op == null)
            {
                op = "";
            }
            string[] lh = this.LuHao.Value.Split(new char[] { '|' });
            string[] pc = this.PiCi.Value.Split(new char[] { '|' });
            string[] zl = this.ZhongLiang.Value.Split(new char[] { '|' });
            string[] zs = this.ZhiShu.Value.Split(new char[] { '|' });

            decimal z,zzl=0;
            int n,zzs=0;
            string sql="";
            int mxs = 0;
            for (int i = 0; i < 20 && i < lh.Length && i < pc.Length && i < zl.Length && i < zs.Length; i++)
            {
                if (lh[i] != "" || pc[i] != "" || zl[i] != "" || zs[i] != "")
                {
                    if (lh[i] == "" || zl[i] == "" || zs[i] == "")
                    {
                        this.Notice = "第 "+(i+1)+" 行记录数据不完整";
                        return;
                    }
                    else if(!decimal.TryParse(zl[i],out z))
                    {
                        this.Notice = "第 " + (i + 1) + " 行记录“重量”录入有误”";
                        return;
                    }
                    else if (!int.TryParse(zs[i], out n))
                    {
                        this.Notice = "第 " + (i + 1) + " 行记录“支数”录入有误”";
                        return;
                    }
                    mxs++;
                    zzl += z;
                    zzs += n;
                    sql += "set @炉号='" + lh[i].Replace("'", "''") + "';";
                    sql += "set @批次='" + pc[i].Replace("'", "''") + "';";
                    sql += "set @重量="+z+";";
                    sql += "set @支数=" + n + ";";
                    sql += "insert into 原料到发明细(原料ID,供货ID,记录ID,炉号,批次,重量,支数,到货日期)values(@原料ID,@供货ID,@记录ID,@炉号,@批次,@重量,@支数,@到货日期);";
                }
            }



            SqlConnection Conn = new SqlConnection();
            Conn.ConnectionString = ConfigurationManager.ConnectionStrings["SCMConnectionString1"].ConnectionString;
            Conn.Open();
            SqlCommand Cmd = new SqlCommand();
            Cmd.Connection = Conn;

            Cmd.CommandText = "declare @车号 varchar(100);declare @备注 nvarchar(50);";
            Cmd.CommandText = Cmd.CommandText + "set @车号='" + this.CheHao.Value.Replace("'", "''") + "';";
            Cmd.CommandText = Cmd.CommandText + "set @备注='" + this.BeiZhu.Value.Replace("'", "''") + "';";

            if (this.Label_ID.Text == "0" || this.Label_ID.Text == "")
            {
                Cmd.CommandText = Cmd.CommandText + "insert into 原料到货记录(原料ID,供货ID,日期,分厂,库区,标重,支数,磅重1,磅重2,明细,库管员,车号,备注)values("
                    + this.LeiBie.Text + "," + this.GongHuo.Text + ",'" + d.ToShortDateString() + "'," + this.FenChang.Text + "," + kq + ","
                    + zzl + "," + zzs + "," + bz1 + "," + bz2 + ","+mxs+",'" + op + "',@车号,@备注);select cast(@@identity as int);";

                this.Label_ID.Text = ((int)Cmd.ExecuteScalar()).ToString();
                Cmd.CommandText = "set nocount on;"
                    + "declare @原料ID int,@供货ID int,@记录ID int,@炉号 varchar(15),@批次 varchar(15),@重量 numeric(18,4),@支数 int,@到货日期 date;"
                    + "set @原料ID=" + this.LeiBie.Text + ";"
                    + "set @供货ID=" + this.GongHuo.Text + ";"
                    + "set @记录ID=" + this.Label_ID.Text + ";"
                    + "set @到货日期='" + d.ToShortDateString() + "';"
                    + sql;

                Cmd.ExecuteNonQuery();
            }
            else if (int.TryParse(this.Label_ID.Text, out n) && n > 0)
            {
                Cmd.CommandText = Cmd.CommandText + "update 原料到货记录 set 原料ID=" + this.LeiBie.Text
                    + ",供货ID=" + this.GongHuo.Text
                    + ",日期='" + d.ToShortDateString()
                    + "',分厂=" + this.FenChang.Text
                    + ",库区=" + kq
                    + ",标重=" + zzl
                    + ",支数=" + zzs
                    + ",磅重1=" + bz1
                    + ",磅重2=" + bz2
                    + ",明细=" + mxs
                    + ",库管员='" + op
                    + "',车号=@车号,备注=@备注 where ID=" + n
                    + ";delete from 原料到发明细 where 记录ID=" + n;
                Cmd.ExecuteNonQuery();
                Cmd.CommandText = "set nocount on;"
                    + "declare @原料ID int,@供货ID int,@记录ID int,@炉号 varchar(15),@批次 varchar(15),@重量 numeric(18,4),@支数 int,@到货日期 date;"
                    + "set @原料ID=" + this.LeiBie.Text + ";"
                    + "set @供货ID=" + this.GongHuo.Text + ";"
                    + "set @记录ID=" + this.Label_ID.Text + ";"
                    + "set @到货日期='" + d.ToShortDateString() + "';"
                    + sql;
                Cmd.ExecuteNonQuery();
            }

            Conn.Close();
            Cmd.Dispose();
            Conn.Dispose();
            this.Notice = "·保存成功";
            this.Button_New.Visible = true;
        }

        protected void Button_Prev_Click(object sender, EventArgs e)
        {
            int p = 0;
            int.TryParse(this.Button_Next.CommandArgument, out p);
            if (p > 0)
            {
                p--;
            }
            Response.Redirect("原料到货记录.aspx?list="+this.Label_List.Text+"&p="+p);
        }

        protected void Button_Next_Click(object sender, EventArgs e)
        {
            int p = 0;
            int.TryParse(this.Button_Next.CommandArgument, out p);
            p++;
            Response.Redirect("原料到货记录.aspx?list=" + this.Label_List.Text + "&p=" + p);
        }

        protected void Button_Send_Click(object sender, EventArgs e)
        {
            Response.Redirect("原料发货明细.aspx?list=" + this.Label_List.Text);
        }

        protected void Button_New_Click(object sender, EventArgs e)
        {
            Response.Redirect("原料到货记录.aspx");
        }
    }
}
